package com.yan.practice;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author taoge
 * @Date 2024/8/15 20:08
 * @Description
 */
public class Practice2 {
    // 写入
    public static void main1(String[] args) throws IOException {
        List<Student1> list = new LinkedList<>();
        list.add(new Student1("08001" , 32 , "0800101" , "赵明" , "1985-12-23"));
        list.add(new Student1("08002" , 28 , "0800201" , "胡广飞" , "1985-10-05"));
        // 创建文件
        Document document = DocumentHelper.createDocument();
        Element element = document.addElement("List");
        Element student = element.addElement("Student");
        Element classId = student.addElement("classId");
        classId.addText("08001");
        Element stuCount = student.addElement("stuCount");
        stuCount.addText("32");
        Element stuId = student.addElement("stuId");
        stuId.addText("0800101");
        student.addElement("name").addText("赵明");
        student.addElement("birthday").addText("1985-12-23");
        Element student1 = element.addElement("Student");
        student1.addElement("classId").addText("08002");
        student1.addElement("stuCount").addText("28");
        student1.addElement("stuId").addText("0800201");
        student1.addElement("name").addText("胡广飞");
        student1.addElement("birthday").addText("1985-10-05");

        OutputFormat prettyPrint = OutputFormat.createPrettyPrint();
        prettyPrint.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileWriter("day20_code/src/com/yan/practice/Student.XML") , prettyPrint);
        xmlWriter.write(document);
        xmlWriter.close();
    }

    // 读出

    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        // 读取文件
        Document document = reader.read("day20_code/src/com/yan/practice/Student.XML");
        // 获得根节点
        Element rootElement = document.getRootElement();
        // 迭代器遍历
        Iterator<Element> iterator = rootElement.elementIterator();
        LinkedList<Student1> list = new LinkedList<>();
        while(iterator.hasNext()){
            Constructor<Student1> cons = Student1.class.getConstructor(String.class, Integer.class, String.class, String.class, String.class);
            Element ziElement = iterator.next();
            // 取出子对象的数值
            String classId = ziElement.element("classId").getText();
            Integer stuCount = Integer.parseInt( ziElement.element("stuCount").getText());
            String stuId = ziElement.element("stuId").getText();
            String name = ziElement.element("name").getText();
            String birthday = ziElement.element("birthday").getText();
            // 存入反射创建的对象中
            Student1 student = cons.newInstance(classId, stuCount, stuId, name, birthday);
            // 存入链表
            list.add(student);
        }
        list.forEach(item-> System.out.println(item));
    }


}

class Student1{
    private String classId;
    private Integer stuCount;
    private String stuId;
    private String name;
    private String birth;

    public Student1(String classId, Integer stuCount, String stuId, String name, String birth) {
        this.classId = classId;
        this.stuCount = stuCount;
        this.stuId = stuId;
        this.name = name;
        this.birth = birth;
    }

    public Student1() {
    }

    public String getClassId() {
        return classId;
    }

    public void setClassId(String classId) {
        this.classId = classId;
    }

    public Integer getStuCount() {
        return stuCount;
    }

    public void setStuCount(Integer stuCount) {
        this.stuCount = stuCount;
    }

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student1 student1 = (Student1) o;

        if (classId != null ? !classId.equals(student1.classId) : student1.classId != null) return false;
        if (stuCount != null ? !stuCount.equals(student1.stuCount) : student1.stuCount != null) return false;
        if (stuId != null ? !stuId.equals(student1.stuId) : student1.stuId != null) return false;
        if (name != null ? !name.equals(student1.name) : student1.name != null) return false;
        return birth != null ? birth.equals(student1.birth) : student1.birth == null;
    }

    @Override
    public int hashCode() {
        int result = classId != null ? classId.hashCode() : 0;
        result = 31 * result + (stuCount != null ? stuCount.hashCode() : 0);
        result = 31 * result + (stuId != null ? stuId.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (birth != null ? birth.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "classId='" + classId + '\'' +
                ", stuCount=" + stuCount +
                ", stuId='" + stuId + '\'' +
                ", name='" + name + '\'' +
                ", birth='" + birth + '\'' +
                '}';
    }
}
